=begin rdoc
  Crappy auto-emo poetry generator
=end

require 'rubygems'
require "hpricot"
require "open-uri"
require "pp"

# Returns an array of lines of poetry
def snarf_poetry(url)
  lines = []
  
  # Load the source page
  source = Hpricot(open(url))

  poem = source.search("//div[@id='rich_content']").inner_html
  # poem.split('\n').each do |line|
  poem.split('<br />').each do |line|
    #puts line.gsub(/<.*?>|&nbsp;|\n|\r/, '').chomp
    lines << line.gsub(/<.*?>|&nbsp;|\n|\r/, '').chomp
  end
  lines.delete_if {|e| e.empty? || e.nil?}
end

def get_random_poem
  emo_power = "http://allpoetry.com/Poem/950"
  emo_power << rand(1000).to_s
  snarf_poetry(emo_power)
end

poems = []

# Get 5 poems and hit them together
5.times do
  # Get a non-empty poem (I'm not really sure why this happens sometimes)
  rp = get_random_poem
  until rp.empty? == false
    rp = get_random_poem
  end
  poems << rp
end

poem = []

poems.each do |p|
  poem << p[rand(p.size)]
end

puts poem